//************************************************************************
//Developed by Guo Ming Li 2009/2010
//Source code:
//  The source code may be freely modified or used but this credit section may not be removed 
//  The original author must be notified of any modifications plus a copy of 
//  the changed source code
//Contact info: mailto://gming.li@gmail.com
//************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;

namespace GML.BizTalk.OperationManager.Library
{
    public class SendPorts
    {
        public static void DeleteSendPorts(string btsDbServerName, string btsMgntDbName, string sBtsAppName)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            List<SendPort> sendPorts = new List<SendPort>();

            try
            {
                root.ConnectionString = Helper.GetConnectionString(btsDbServerName, btsMgntDbName);

                foreach (SendPort sendPort in root.SendPorts)
                {
                    sendPorts.Add(sendPort);
                }
                //Remove a receive port.
                foreach (SendPort sendPort in sendPorts)
                {
                    if (sendPort.Application.Name == sBtsAppName)
                    {
                        root.RemoveSendPort(sendPort);
                    }
                }

                //Try to commit the changes made so far. 
                root.SaveChanges();
            }
            catch (Exception e)//If it fails, roll-back everything we have done so far
            {
                System.Diagnostics.Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                Logger.doLog(Guid.NewGuid().ToString(), e.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), e.StackTrace, Logger.ServerityLevel.ERROR);
                root.DiscardChanges();
                throw e;
            }
            finally
            {
                if (root != null) root.Dispose();
                root = null;
                sendPorts = null;
            }
        }

        public static bool StartAllSendPorts(string btsDbServerName, string btsMgntDbName, string sBtsAppName, out Exception ex)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            List<SendPort> sendPorts = new List<SendPort>();

            try
            {
                root.ConnectionString = Helper.GetConnectionString(btsDbServerName, btsMgntDbName);

                foreach (SendPort sendPort in root.SendPorts)
                {
                    if (sendPort.Application.Name == sBtsAppName)
                    {
                        if (!sendPort.Status.Equals(PortStatus.Started))
                        {
                            sendPort.Status = PortStatus.Started;
                        }
                    }
                }

                //Try to commit the changes made so far. 
                root.SaveChanges();
                ex = null;
                return true;
            }
            catch (Exception e)//If it fails, roll-back everything we have done so far
            {
                System.Diagnostics.Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                Logger.doLog(Guid.NewGuid().ToString(), e.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), e.StackTrace, Logger.ServerityLevel.ERROR);
                root.DiscardChanges();
                ex = e;
                return false;
            }
            finally
            {
                if (root != null) root.Dispose();
                root = null;
                sendPorts = null;
            }
        }

        public static SendPortCollection GetSendPorts(List<string> listOfSendPortNames, string btsDbServerName, string btsMgntDbName)
        {
            BtsCatalogExplorer root = new BtsCatalogExplorer();
            List<SendPort> sendPorts = new List<SendPort>();
            SendPortCollection sendPortCollection = new SendPortCollection();

            try
            {
                root.ConnectionString = Helper.GetConnectionString(btsDbServerName, btsMgntDbName);
                foreach (Application app in root.Applications)
                {
                    if (app.Name == "ArielBPM.Butterfly")
                    {
                        foreach (SendPort sendPort in app.SendPorts)
                        {
                            sendPorts.Add(sendPort);
                        }
                    }
                }
                //Remove a receive port.
                foreach (SendPort sendPort in sendPorts)
                {
                    foreach (string sendPortName in listOfSendPortNames)
                    {
                        if (sendPortName.Equals(sendPort.Name))
                        {
                            root.RemoveSendPort(sendPort);
                            break;
                        }
                    }
                }

                sendPortCollection = root.SendPorts;
                root.DiscardChanges();

                //Try to commit the changes made so far. 
                //root.SaveChanges();
                return sendPortCollection;
            }
            catch (Exception e)//If it fails, roll-back everything we have done so far
            {
                System.Diagnostics.Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                Logger.doLog(Guid.NewGuid().ToString(), e.Message, Logger.ServerityLevel.ERROR);
                Logger.doLog(Guid.NewGuid().ToString(), e.StackTrace, Logger.ServerityLevel.ERROR);
                root.DiscardChanges();
                throw e;
            }
            finally
            {
                if (root != null) root.Dispose();
                root = null;
                sendPorts = null;
            }
        }

    }
}
